<?php
namespace app\home\controller;
use app\common\controller\Table;
use houdunwang\arr\Arr;

Class TableMenuIndex extends Table{

    protected $table_header = [
        ['key'=>'id','name'=>'编码','search'=>0],
        ['key'=>'pid','name'=>'父级id','search'=>0],
        ['key'=>'_name','name'=>'菜单命名','search'=>1,'width'=>200,'align'=>'left'],
        ['key'=>'url','name'=>'链接地址','search'=>0,'width'=>200,'align'=>'left'],
        ['key'=>'is_hide','name'=>'是否隐藏','templet'=>"switchTpl(d,'is_hide')",'search'=>0],
        ['key'=>'icon','name'=>'图标','search'=>0],
        ['key'=>'module','name'=>'所属模块','search'=>1],
        ['key'=>'controller','name'=>'控制器','search'=>0],
        ['key'=>'is_jump','name'=>'是否跳转','search'=>0],
        ['key'=>'is_external','name'=>'是否是外部链接','search'=>0],
        ['key'=>'sort','name'=>'排序','search'=>0],
        ['key'=>'create_time','name'=>'创建时间','search'=>0],
    ];

    public function data($page=1,$limit=10){
        $this->is_pri('read');
        $params = $this->params;
        $where = [];$this->count = 0;
        if(isset($params['page'])&&isset($params['limit'])){
            $page = $params['page'];$limit = $params['limit'];
        }
        if($limit>1000){
            $res = ['code'=>1,'msg'=>'请求数据数量过多！'];
        }
        else{
            $res = obj_decode( $this->page($page,$limit,$where,'sort desc,id desc') );
            if($res['code']){
                $res['code']=0;
                $res['count'] = $res['total'];
                $this->count = $res['total'];
                $res['data'] = Arr::tree($res['data'],'name',$fieldPri = 'id', $fieldPid = 'pid');
            }
            else{
                $res['code']=1;
            }
        }

        foreach ($res['data'] as $k=>$v){
            $icon = "<i class='{$v['icon']}'></i>";
            $res['data'][$k]['icon'] = $icon;
            if($v['is_jump']==1){
                $res['data'][$k]['is_jump'] = '是';
            }
            else{
                $res['data'][$k]['is_jump'] = '否';
            }
            if($v['is_external']==1){
                $res['data'][$k]['is_external'] = '是';
            }
            else{
                $res['data'][$k]['is_external'] = '否';
            }
        }
        return json($res);//已经是结果 ajax
    }

    public function index(){
        parent::index();
        $table_pri =$this->privilege;
        $table_pri['read'] = 0;
        $this->assign([
            'table_header'=>$this->table_header,
            'pri'=>$table_pri
        ]);
        return $this->fetch();
    }

    public function recycleData($page=1,$limit=15){
        parent::recycleData($page,$limit);
        $res = $this->data;
        foreach ($res['data'] as $k=>$v){
            $dom = "<i class='{$v['icon']}'></i>";
            $res['data'][$k]['icon'] = $dom;
        }
        return json( $res );
    }

    public function recycleindex()
    {
        parent::recycleindex();
        $this->assign([
            'table_header'=> $this->table_header
        ]);
        return $this->fetch();
    }


//
//    public function tableUpdate($is_update = 1)
//    {
//        parent::tableUpdate($is_update);
//
//    }
}